#include <QApplication>
#include <QLabel>
#include <QString>
#include <rclcpp/rclcpp.hpp>
#include <status_interfaces/msg/system_status.hpp>

class SysStatusDisplay : public rclcpp::Node
{
    private:

    // 订阅者智能指针
    rclcpp::Subscription<status_interfaces::msg::SystemStatus>::SharedPtr subscriber_; 
    QLabel* label_= new QLabel();
    public:
        explicit SysStatusDisplay():Node("sys_status_display")
        {
            subscriber_ =this->create_subscription<status_interfaces::msg::SystemStatus>("/sys_status",10,[&]
                (const status_interfaces::msg::SystemStatus::SharedPtr msg) -> void{
                    label_->setText(get_qstr_from_msg(msg));
                });
            
            label_->setText(get_qstr_from_msg(std::make_shared<status_interfaces::msg::SystemStatus>()));
            label_->show();
        };

        QString get_qstr_from_msg(const status_interfaces::msg::SystemStatus::SharedPtr msg )
        {
            std::stringstream show_str;
            show_str <<"============系统状态显示工具============\n"
                    <<"数 据 时 间\t"<<msg->stamp.sec<<"\ts\n"
                    <<"主 机 名 字:\t"<<msg->host_name<<"\t\n"
                    <<"CPU 使用率:\t"<<msg->cpu_percent<<"\t%\n"
                    <<"内存使用率:\t"<<msg->memory_percent<<"\t%\n"
                    <<"内存总大小:\t"<<msg->memory_tolal<<"\tMB\n"
                    <<"剩 余 内 存:\t"<<msg->memory_available<<"\tMB\n"
                    <<"网络发送量:\t"<<msg->net_sent<<"\tMB\n"
                    <<"网络接收量:\t"<<msg->net_recv<<"\tMB\n"
                    <<"========================================";

            return QString::fromStdString(show_str.str());
        };
} ;      




int main(int argc, char* argv[])
{
    rclcpp::init(argc,argv);
    QApplication app(argc,argv);
    auto node =std::make_shared<SysStatusDisplay>();

    // ros节点线程
    std::thread spin_thread{
        [&]()->void
        {
            rclcpp::spin(node);
        }
    };
    spin_thread.detach();
    app.exec();
    return 0;
}